package com.vlyman.shiro.config;

import com.vlyman.redis.config.JedisConfig;
import com.vlyman.redis.exception.RedisException;
import com.vlyman.redis.utils.JedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.crazycake.shiro.RedisManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.PostConstruct;

/**
 * 类描述：
 *  redis配置类
 * @author chenjs
 * @date 2018/12/29 10:24
 **/
@Slf4j
@Configuration
public class RedisConfig {

    @Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private int port;
    @Value("${redis.password}")
    private String pwd;
    @Value("${redis.timeout:2000}")
    private int timeout;
    @Value("${redis.maxTotal:32}")
    private int maxTotal;
    @Value("${redis.maxIdle:16}")
    private int maxIdle;
    @Value("${redis.minIdle:8}")
    private int minIdle;
    @Value("${redis.maxWaitMillis:10000}")
    private long maxWaitMillis;

    /**
     * 方法描述：
     *   初始化redis
     *  @author chenjs
     *  @date 2018/12/29 09:47
     */
    @PostConstruct
    public void initRedisManager() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setMaxWaitMillis(maxWaitMillis);
        JedisPool jedisPool = JedisConfig.redisPoolFactory(config, host, port, timeout, pwd);
        JedisUtil.init(jedisPool);
    }
}
